
import time, requests

#爬取网站：https://read.douban.com/category/100


for page in range(1, 650):

    cookies = {
        'll': '"108288"',
        'bid': 'MLk2jzJucIo',
        '__utma': '30149280.1009108073.1676363530.1676363530.1676363530.1',
        '__utmc': '30149280',
        '__utmz': '30149280.1676363530.1.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic',
        'ap_v': '0,6.0',
        'gr_user_id': '5f6b3a79-0def-4efa-81cc-ead985a9db1b',
        '_ga': 'GA1.3.1009108073.1676363530',
        '_gid': 'GA1.3.1445175940.1676363665',
        '_ga': 'GA1.1.1009108073.1676363530',
        '_pk_ref.100001.a7dd': '%5B%22%22%2C%22%22%2C1676363665%2C%22https%3A%2F%2Fmarket.douban.com%2F%22%5D',
        '__gads': 'ID=f47c331c82383399-22ce2155cbd900c2:T=1676363668:RT=1676363668:S=ALNI_Ma8ZFHttCYFG53xyrCkq98-JsXHcg',
        '__gpi': 'UID=00000bc1a19c31ad:T=1676363668:RT=1676363668:S=ALNI_MZ9ph6CZ4Z3H1ta1OxZYmeMfj4FSg',
        '_pk_id.100001.a7dd': 'c9147b0043436784.1676363665.1.1676364284.1676363665.',
        '_ga_RXNMP372GL': 'GS1.1.1676367556.2.0.1676367556.60.0.0',
    }

    headers = {
        'Accept': 'application/json',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Connection': 'keep-alive',
        'Content-Type': 'application/json',
        # 'Cookie': 'll="108288"; bid=MLk2jzJucIo; __utma=30149280.1009108073.1676363530.1676363530.1676363530.1; __utmc=30149280; __utmz=30149280.1676363530.1.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; ap_v=0,6.0; gr_user_id=5f6b3a79-0def-4efa-81cc-ead985a9db1b; _ga=GA1.3.1009108073.1676363530; _gid=GA1.3.1445175940.1676363665; _ga=GA1.1.1009108073.1676363530; _pk_ref.100001.a7dd=%5B%22%22%2C%22%22%2C1676363665%2C%22https%3A%2F%2Fmarket.douban.com%2F%22%5D; __gads=ID=f47c331c82383399-22ce2155cbd900c2:T=1676363668:RT=1676363668:S=ALNI_Ma8ZFHttCYFG53xyrCkq98-JsXHcg; __gpi=UID=00000bc1a19c31ad:T=1676363668:RT=1676363668:S=ALNI_MZ9ph6CZ4Z3H1ta1OxZYmeMfj4FSg; _pk_id.100001.a7dd=c9147b0043436784.1676363665.1.1676364284.1676363665.; _ga_RXNMP372GL=GS1.1.1676367556.2.0.1676367556.60.0.0',
        'Origin': 'https://read.douban.com',
        'Referer': 'https://read.douban.com/category/100?sort=hot&sale=&page=1',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.41',
        'X-CSRF-Token': 'null',
        'X-Requested-With': 'XMLHttpRequest',
        'sec-ch-ua': '"Chromium";v="110", "Not A(Brand";v="24", "Microsoft Edge";v="110"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
    }

    json_data = {
        'sort': 'hot',
        'page': page,
        'kind': 100,
        'query': '\n    query getFilterWorksList($works_ids: [ID!]) {\n      worksList(worksIds: $works_ids) {\n        \n    \n    title\n    cover(useSmall: false)\n    url\n    isBundle\n    coverLabel(preferVip: true)\n  \n    \n  url\n  title\n\n    \n  author {\n    name\n    url\n  }\n  origAuthor {\n    name\n    url\n  }\n  translator {\n    name\n    url\n  }\n\n    \n  abstract\n  authorHighlight\n  editorHighlight\n\n    \n    isOrigin\n    kinds {\n      \n    name @skip(if: true)\n    shortName @include(if: true)\n    id\n  \n    }\n    ... on WorksBase @include(if: true) {\n      wordCount\n      wordCountUnit\n    }\n    ... on WorksBase @include(if: false) {\n      inLibraryCount\n    }\n    ... on WorksBase @include(if: false) {\n      \n    isEssay\n    \n    ... on EssayWorks {\n      favorCount\n    }\n  \n    \n    \n    averageRating\n    ratingCount\n    url\n    isColumn\n    isFinished\n  \n  \n  \n    }\n    ... on EbookWorks @include(if: false) {\n      \n    ... on EbookWorks {\n      book {\n        url\n        averageRating\n        ratingCount\n      }\n    }\n  \n    }\n    ... on WorksBase @include(if: false) {\n      isColumn\n      isEssay\n      onSaleTime\n      ... on ColumnWorks {\n        updateTime\n      }\n    }\n    ... on WorksBase @include(if: true) {\n      isColumn\n      ... on ColumnWorks {\n        isFinished\n      }\n    }\n    ... on EssayWorks {\n      essayActivityData {\n        \n    title\n    uri\n    tag {\n      name\n      color\n      background\n      icon2x\n      icon3x\n      iconSize {\n        height\n      }\n      iconPosition {\n        x y\n      }\n    }\n  \n      }\n    }\n    highlightTags {\n      name\n    }\n    ... on WorksBase @include(if: false) {\n      fanfiction {\n        tags {\n          id\n          name\n          url\n        }\n      }\n    }\n  \n    \n  ... on WorksBase {\n    copyrightInfo {\n      newlyAdapted\n      newlyPublished\n      adaptedName\n      publishedName\n    }\n  }\n\n    isInLibrary\n    ... on WorksBase @include(if: false) {\n      \n    fixedPrice\n    salesPrice\n    isRebate\n  \n    }\n    ... on EbookWorks {\n      \n    fixedPrice\n    salesPrice\n    isRebate\n  \n    }\n    ... on WorksBase @include(if: true) {\n      ... on EbookWorks {\n        id\n        isPurchased\n        isInWishlist\n      }\n    }\n    ... on WorksBase @include(if: false) {\n      fanfiction {\n        fandoms {\n          title\n          url\n        }\n      }\n    }\n    ... on WorksBase @include(if: false) {\n      fanfiction {\n        kudoCount\n      }\n    }\n  \n        id\n        isOrigin\n      }\n    }\n  ',
        'variables': {},
    }

    response = requests.post('https://read.douban.com/j/kind/', cookies=cookies, headers=headers, json=json_data)

    response_json = response.json()

    for i in response_json['list']:

        title = i['title']

        if not i['origAuthor']:
            author = i['author'][0]['name']

        if not i['author']:
            author = i['origAuthor'][0]['name']

        print(f'书名：{title}\t作者：{author}')

    time.sleep(5)

